package com.sjq.common.util;

import java.util.Hashtable;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlUtils {
	
	//返回新疆协查人员URL
	public static String  xmlToStringXjryxc(String result){	
		Document doc;
		try {
			doc = DocumentHelper.parseText(result);
			Element RBSPMessage=doc.getRootElement();
			Element Value=RBSPMessage.element("Method").element("Items").element("Item").element("Value");//
			List rows=	Value.elements();
			int rows_lenght= rows.size();//
			if(rows_lenght>2){
				Element row=(Element)rows.get(2);//
				List row_titles=row.elements();//
				Element data=(Element)row_titles.get(0);
				String dataText=data.getText();
				return dataText;
			}else {
				return "http://10.20.32.139:9080/Exhibition/jcxx/Error.do";
			}
		} catch (Exception e) {
			return "http://10.20.32.139:9080/Exhibition/jcxx/Error.do";
		}
	}
	public static JSONObject xmlToStringQgck(String result){
		JSONObject json=new JSONObject();
		JSONArray jsonA=new JSONArray();
		JSONObject jsonData=new JSONObject();
		String p[]={"","",""};
		Document doc;
		try {
			doc = DocumentHelper.parseText(result);
			Element RBSPMessage=doc.getRootElement();
			Element Value=RBSPMessage.element("Method").element("Items").element("Item").element("Value");//
			List rows=	Value.elements();
			Element row_code=(Element)rows.get(0);//解析状态码
			List row_datas=row_code.elements();//
			String statusCode=((Element)row_datas.get(0)).getText();//状态码
			Element row_items=(Element)rows.get(1);//字段项
			List row_titles=row_items.elements();//
			Element row_data=(Element)rows.get(2);//数据内容
			List row_data_value=row_data.elements();
			//System.out.println(statusCode);
			if(statusCode.equals("000")){
				//获取成功时
				jsonData.put("status", "ok");
				for(int i=0;i<row_data_value.size();i++){
					String key=((Element)row_titles.get(i)).getText();
					String value=((Element)row_data_value.get(i)).getText();
					if(value.equals("")) value="无数据";
					
					jsonData.put(key, value);
				}
				return jsonData;
			}else {
				jsonData.put("status", "error");
				return jsonData;
			}
			
			
		} catch (Exception e) {
			//e.printStackTrace();
			jsonData.put("status", "error");
			return jsonData;
		}
	}
	
	//解析全国集合
	public static JSONArray xmlToListQgck(String result){
		JSONObject json=new JSONObject();
		JSONArray jsonA=new JSONArray();
		JSONObject jsonData=new JSONObject();
		String p[]={"","",""};
		Document doc;
		try {
			doc = DocumentHelper.parseText(result);
			Element RBSPMessage=doc.getRootElement();
			Element Value=RBSPMessage.element("Method").element("Items").element("Item").element("Value");//
			List rows=	Value.elements();
			Element row_code=(Element)rows.get(0);//解析状态码
			List row_datas=row_code.elements();//
			String statusCode=((Element)row_datas.get(0)).getText();//状态码
			Element row_items=(Element)rows.get(1);//字段项
			List row_titles=row_items.elements();//
			//System.out.println(statusCode);
			if(statusCode.equals("000")){
				for(int i=2;i<rows.size();i++){
					Element row_data=(Element)rows.get(i);//数据内容
					List row_data_value=row_data.elements();
					for(int j=0;j<row_data_value.size();j++){			
					String key=	((Element)row_titles.get(j)).getText();			
					String value=((Element)row_data_value.get(j)).getText();	
					jsonData.put(key, value);
					}
					jsonA.add(jsonData);
				}
				
			
			
			}else {
				jsonData.put("status", "error");
				return jsonA;
			}
			
			return jsonA;
		} catch (Exception e) {
			//e.printStackTrace();
			jsonData.put("status", "error");
			return jsonA;
		}
	}
	
	/**解析世纪桥的版本**/
	public static JSONObject xmlToStringQgckByCbs(String result){
		JSONObject json=new JSONObject();
		JSONArray jsonA=new JSONArray();
		JSONObject jsonData=new JSONObject();
		String p[]={"","",""};
		Document doc;
		try {
			doc = DocumentHelper.parseText(result);
			Element RBSPMessage=doc.getRootElement();
			Element Value=RBSPMessage.element("Method").element("Items").element("Item").element("Value");//
			List rows=	Value.elements();
			Element row_code=(Element)rows.get(0);//字段名
			List row_datas=row_code.elements();//
			Element row_items=(Element)rows.get(0);//字段项
			List row_titles=row_items.elements();//
			Element row_data=(Element)rows.get(3);//数据内容
			List row_data_value=row_data.elements();
			//System.out.println(statusCode);
				//获取成功时
				jsonData.put("status", "ok");
				for(int i=0;i<row_data_value.size();i++){
					String key=((Element)row_titles.get(i)).getText();
					String value=((Element)row_data_value.get(i)).getText();
					if(value.equals("")) value="无数据";
					
					jsonData.put(key, value);
				}
				return jsonData;
			
		} catch (Exception e) {
			//e.printStackTrace();
			jsonData.put("status", "error");
			return jsonData;
		}
	}
	//世纪桥生成html页面
	public static String xmlToHtmlQgckByCbs(String result){

		Document doc;
		try {
			doc = DocumentHelper.parseText(result);
			Element RBSPMessage=doc.getRootElement();
			Element Value=RBSPMessage.element("Method").element("Items").element("Item").element("Value");//
			List rows=	Value.elements();
			Element row_code=(Element)rows.get(0);//字段名
			List row_datas=row_code.elements();//
			Element row_items=(Element)rows.get(1);//字段中文名称
			List row_titles=row_items.elements();//
			Element row_data=(Element)rows.get(3);//数据内容
			List row_data_value=row_data.elements();
			//System.out.println(statusCode);
			String html="";
		
				for(int i=0;i<row_data_value.size();i++){
					String key=((Element)row_titles.get(i)).getText();
					String value=((Element)row_data_value.get(i)).getText();
					if(value.equals("")) value="无数据";
					html=html+"<div class='dingke-search-show'><font  style='color: #1ab394;'>"+key+":</font><font></font>"+value+"</div>";
				}
				return html;
			
		} catch (Exception e) {
			//e.printStackTrace();
		
			return "<p>未查到数据</p>";
		}
	}
	
	/**多条记录版本***/
	public static JSONArray xmlToListQgckByCbs(String result){
		JSONObject json=new JSONObject();
		JSONArray jsonA=new JSONArray();
		JSONObject jsonData=new JSONObject();
		String p[]={"","",""};
		Document doc;
		try {
			doc = DocumentHelper.parseText(result);
			Element RBSPMessage=doc.getRootElement();
			Element Value=RBSPMessage.element("Method").element("Items").element("Item").element("Value");//
			List rows=	Value.elements();
			Element row_code=(Element)rows.get(0);//字段名英文
			List row_datas=row_code.elements();//
			Element row_items=(Element)rows.get(1);//字段名中文
			List row_titles=row_items.elements();//
			
			for(int i=3;i<rows.size();i++){
				Element row_data=(Element)rows.get(i);//数据内容
				List row_data_value=row_data.elements();
				for(int j=0;j<row_data_value.size();j++){			
				String key=	((Element)row_datas.get(j)).getText();			
				String value=((Element)row_data_value.get(j)).getText();	
				jsonData.put(key, value);
				}
				jsonA.add(jsonData);
			}
			
			return jsonA;
			//System.out.println(statusCode);
		
			
		} catch (Exception e) {
			//e.printStackTrace();
			jsonData.put("status", "error");
			return jsonA;
		}
	}
	public static JSONObject translateDicBySjq(JSONObject jsonData){
		if(!jsonData.getString("WHCD").equals("")){
			String sql_WHCD="( select ITEM1 from giapsys.dic_items where dic_name='文化程度' and code='"+jsonData.getString("WHCD")+"' ) as WHCD";
			
		}
		
		String sql="select ( select ITEM1 from giapsys.dic_items where dic_name='文化程度' and code='"+jsonData.getString("WHCD")+"' ) as WHCD, " +
				" ( select ITEM1 from giapsys.dic_items where dic_name='性别' and code='"+jsonData.getString("XB")+"' ) as XB," +
				" ( select ITEM1 from giapsys.dic_items where dic_name='民族' and code='"+jsonData.getString("MZ")+"') as MZ," +
				" ( select ITEM1 from giapsys.dic_items where dic_name='婚姻状况' and code='"+jsonData.getString("HYZK")+"') as HYZK," +
				" ( select ITEM1 from giapsys.dic_items where dic_name='兵役状况' and code='"+jsonData.getString("BYQK")+"') as BYQK," +
				" (select ITEM1 from giapsys.dic_items where dic_name='行政区划' and code = '"+jsonData.getString("HKSZD")+"') AS HKSZD," +
				" (select ITEM1 from giapsys.dic_items where dic_name='出生地' and code = '"+jsonData.getString("CSD")+"') AS CSD," +
				" (select ITEM1 from giapsys.dic_items where dic_name='籍贯省市县区' and code = '"+jsonData.getString("JGSSX")+"') AS JGSSX," +
				" (select ITEM1 from giapsys.dic_items where dic_name='职业类别' and code = '"+jsonData.getString("ZY")+"') AS ZY " +
				" FROM DUAL";
		ConnDbJw jsdb=new ConnDbJw();
		Hashtable hash=jsdb.getValue(sql);
		String WHCD=StringUtil.notEmpty((String)hash.get("WHCD"));
		String XB=StringUtil.notEmpty((String)hash.get("XB"));
		String MZ=StringUtil.notEmpty((String)hash.get("MZ"));
		String HYZK=StringUtil.notEmpty((String)hash.get("HYZK"));
		String BYQK=StringUtil.notEmpty((String)hash.get("BYQK"));
		String HKSZD=StringUtil.notEmpty((String)hash.get("HKSZD"));
		String CSD = StringUtil.notEmpty((String)hash.get("CSD"));
		String JGSSX=StringUtil.notEmpty((String)hash.get("JGSSX"));
		String ZY=StringUtil.notEmpty((String)hash.get("ZY"));
		jsonData.put("HYZK", HYZK);
		jsonData.put("XB", XB);
		jsonData.put("WHCD", WHCD);
		jsonData.put("BYQK", BYQK);
		jsonData.put("MZ", MZ);
		jsonData.put("HKSZD", HKSZD);
		jsonData.put("CSD", CSD);
		jsonData.put("ZY", ZY);
		jsonData.put("JGSSX", JGSSX);
		
		return jsonData;
		//jString WHCD=(String)hash.get("WHCD");sonData.size("BYQK","");
	
	}
	
	
	//翻译全国数据字典代码
	public static JSONObject translateDic(JSONObject jsonData){
		String sql="select ( select ITEM1 from giapsys.dic_items where dic_name='文化程度' and code='"+jsonData.getString("WHCD")+"' ) as WHCD, " +
				" ( select ITEM1 from giapsys.dic_items where dic_name='性别' and code='"+jsonData.getString("XB")+"' ) as XB," +
				" ( select ITEM1 from giapsys.dic_items where dic_name='民族' and code='"+jsonData.getString("MZ")+"') as MZ," +
				" ( select ITEM1 from giapsys.dic_items where dic_name='婚姻状况' and code='"+jsonData.getString("HYZK")+"') as HYZK," +
				" ( select ITEM1 from giapsys.dic_items where dic_name='兵役状况' and code='"+jsonData.getString("BYQK")+"') as BYQK," +
				" (select ITEM1 from giapsys.dic_items where dic_name='行政区划' and code = '"+jsonData.getString("HKSZD")+"') AS HKSZD," +
				" (select ITEM1 from giapsys.dic_items where dic_name='出生地' and code = '"+jsonData.getString("CSD")+"') AS CSD," +
				" (select ITEM1 from giapsys.dic_items where dic_name='籍贯省市县区' and code = '"+jsonData.getString("JGSSX")+"') AS JGSSX," +
				" (select ITEM1 from giapsys.dic_items where dic_name='职业类别' and code = '"+jsonData.getString("ZY")+"') AS ZY " +
				" FROM DUAL";
		ConnDbJw jsdb=new ConnDbJw();
		Hashtable hash=jsdb.getValue(sql);
		String WHCD=StringUtil.notEmpty((String)hash.get("WHCD"));
		String XB=StringUtil.notEmpty((String)hash.get("XB"));
		String MZ=StringUtil.notEmpty((String)hash.get("MZ"));
		String HYZK=StringUtil.notEmpty((String)hash.get("HYZK"));
		String BYQK=StringUtil.notEmpty((String)hash.get("BYQK"));
		String HKSZD=StringUtil.notEmpty((String)hash.get("HKSZD"));
		String CSD = StringUtil.notEmpty((String)hash.get("CSD"));
		String JGSSX=StringUtil.notEmpty((String)hash.get("JGSSX"));
		String ZY=StringUtil.notEmpty((String)hash.get("ZY"));
		jsonData.put("HYZK", HYZK);
		jsonData.put("XB", XB);
		jsonData.put("WHCD", WHCD);
		jsonData.put("BYQK", BYQK);
		jsonData.put("MZ", MZ);
		jsonData.put("HKSZD", HKSZD);
		jsonData.put("CSD", CSD);
		jsonData.put("ZY", ZY);
		jsonData.put("JGSSX", JGSSX);
		
		return jsonData;
		//jString WHCD=(String)hash.get("WHCD");sonData.size("BYQK","");
	
	}
	
}
